package  org.wak.utils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Timer;

	public class BitmapMovieClip extends MovieClip
	{
		private var mFramesArray:Array ;
		private var mCurrentFrame:int = 0 ;
		private var mFrameDelay:Number = 100 ;
		private var mLoopTimes:int = 0 ;
		private var mAnimTimer:Timer ;
		
		/**
		 * Dispatched when the loop reachs the end. 
		 */		
		public static const ANIM_COMPLETE_EVENT:String = "anim_complete_event";
		
		public function BitmapMovieClip(pBmp:Bitmap, pRows:uint = 1, pColumns:uint = 1)
		{
			super();
			
			mFramesArray = new Array( pRows * pColumns);
			
			var bmd1:BitmapData = null;
			var rect:Rectangle = null;
			var pt:Point = null; 
			
			
			var w:int = pBmp.width / pColumns ;
			var h:int = pBmp.height / pRows ;
			
			var index:int ;
			
			for(var r:int = 0; r <  pRows ; r++)
			{
				for(var c:int = 0; c < pColumns ; c++)
				{
					
					bmd1 = new BitmapData(w, h, true, 0x000000FF);
					rect = new Rectangle(c * w, r * h, w, h);
					pt = new Point(0, 0); 
					
					bmd1.copyPixels(pBmp.bitmapData,rect,pt);	
					
					index = (r * pColumns) + c ;
					mFramesArray[ index ] = new Bitmap(bmd1);
					
				}				
			}
			
			SetFrames();
			//this.addEventListener(Event.ENTER_FRAME,Update,false,0,true);
			
			
			mAnimTimer = new Timer(mFrameDelay, mLoopTimes);
			mAnimTimer.addEventListener(TimerEvent.TIMER, Update,false,0,true );
			this.addEventListener(Event.REMOVED_FROM_STAGE,OnRemovedFromStage,false,0,true);
			
		}
		
		public function set frameDelay(value:Number):void
		{
			mFrameDelay = value ;
			mAnimTimer.delay = mFrameDelay ; 
		}
		
		public function get frameDelay():Number		
		{
			return mFrameDelay ;
		}
		
		public function set loopTimes(value:int):void
		{
			mLoopTimes = value ;
			mAnimTimer.repeatCount = mLoopTimes ;
		}
		
		public function get loopTimes():int
		{
			return mLoopTimes ;
		}
		
		public override function play():void
		{
			//trace("play:"+mFramesArray.length);
			mAnimTimer.reset();
			mAnimTimer.start();
			
			if(!this.hasEventListener(Event.REMOVED_FROM_STAGE))
			{
				this.addEventListener(Event.REMOVED_FROM_STAGE,OnRemovedFromStage,false,0,true);
			}	
		}  
		
		public override function stop():void
		{
		 	mAnimTimer.stop() ;			
		}
		
		private function Update(pEv:Event):void
		{
			nextFrame();
		}
		
		public override function nextFrame():void
		{
			var prevFrame:int  = mCurrentFrame  ;
			var currentFrame:int	= mCurrentFrame + 1;
			
			if( currentFrame > (mFramesArray.length - 1) )
			{
				currentFrame = 0 ;
				if(this.hasEventListener( ANIM_COMPLETE_EVENT ) )
					this.dispatchEvent( new Event(ANIM_COMPLETE_EVENT) );
			}
			
			var prevBmp:Bitmap = mFramesArray[prevFrame] ;
			var curBmp:Bitmap = mFramesArray[currentFrame];
			
			if( this.contains( prevBmp ) )
			{
				this.removeChild( prevBmp );								
			}
			
			this.addChild( curBmp );
			mCurrentFrame = currentFrame ;		
		}
		
		public override function get currentFrame():int
		{
			return mCurrentFrame ;
		}
		
		public function set currentFrame(value:int):void
		{
			var prevFrame:int  = mCurrentFrame  ;
			var currentFrame:int = value;
			
			if( currentFrame > (mFramesArray.length - 1) )
			{
				currentFrame = 0 ;
			}
			
			var prevBmp:Bitmap = mFramesArray[prevFrame] ;
			var curBmp:Bitmap = mFramesArray[currentFrame];
			
			if( this.contains( prevBmp ) )
			{
				this.removeChild( prevBmp );								
			}
			
			this.addChild( curBmp );
			mCurrentFrame = currentFrame ;	
		}
		
		private function SetFrames():void
		{
			
			var b:Bitmap ;			
			for(var i:int = 0; i < mFramesArray.length; i++)
			{
				b = mFramesArray[i] as Bitmap ;				
				this.addChild(b);				
				this.nextFrame() ;
				this.width = b.width ;
				this.height = b.height ;
			}		
			
		}
		
		// function only for testing.
		private function testAdd():void
		{
			var b:Bitmap ;
			var row:int = 0;
			var col:int = 0;
						
			for(var i:int = 0; i < mFramesArray.length; i++)
			{
				b = mFramesArray[i] as Bitmap ;
				b.x = b.width * col ;
				b.y = b.height * row ;
				
				
				if(col >= 3)
				{
					col = 0;
					row++ ;
				}
				else
				{
					col++ ;
				}
				
				
				this.addChild(b);
			}			
		}
		
		private function OnRemovedFromStage(event:Event):void
		{
			this.removeEventListener(Event.REMOVED_FROM_STAGE,OnRemovedFromStage);
			mAnimTimer.stop() ;
		}
		
	}
}